//mine.js
//获取应用实例
const app = getApp()

Page({
  data: {
    img:"/imgs/01a.png",
    orderNum:0,
    storeNum:0,
  },
  getUserInfo:function(e){
    var that=this;
    app.globalData.userInfo = e.detail.userInfo;
    wx.login({
      success: function (response) {
        var code = response.code
        wx.getUserInfo({
          success: function (resp) {
            var uI = resp.userInfo;
            var th = this;
            wx.request({
              url: 'https://soya.91soya.com/onLogin',
              data: {
                code: code,
                iv: resp.iv,
                encryptedData: resp.encryptedData
              },
              success: function (res) {
                app.globalData.openId = res.data.openId;
                wx.request({
                  url: 'http://soya.91soya.com/api/wxcreate',
                  method: "post",
                  data: {
                    openid: res.data.openId,
                    avatarUrl: uI.avatarUrl,
                    country: uI.country,
                    city: uI.city,
                    gender: uI.gender,
                    language: uI.language,
                    nickName: uI.nickName,
                    province: uI.province
                  }
                })
                wx.request({
                  url: 'https://soya.91soya.com/api/ordernum/' + app.globalData.openId,
                  method: "get",
                  success: function (res) {
                    that.setData({
                      orderNum: res.data.ordernum,
                      storeNum: res.data.collnum
                    })
                  }
                })
              }
            })
          }
        })
      },
      fail: function () {

      }
    });
    this.setData({
      userInfo:e.detail.userInfo,
      hasUserInfo:true
    })
  },
  onLoad:function(){
    wx.login({
      success: function (response) {
        var code = response.code
        wx.getUserInfo({
          success: function (resp) {
            var uI = resp.userInfo;
            var th = this;
            wx.request({
              url: 'https://soya.91soya.com/onLogin',
              data: {
                code: code,
                iv: resp.iv,
                encryptedData: resp.encryptedData
              },
              success: function (res) {
                app.globalData.openId = res.data.openId;
                wx.request({
                  url: 'http://soya.91soya.com/api/wxcreate',
                  method: "post",
                  data: {
                    openid: res.data.openId,
                    avatarUrl: uI.avatarUrl,
                    country: uI.country,
                    city: uI.city,
                    gender: uI.gender,
                    language: uI.language,
                    nickName: uI.nickName,
                    province: uI.province
                  }
                })
                wx.request({
                  url: 'https://soya.91soya.com/api/ordernum/' + app.globalData.openId,
                  method:"get",
                  success:function(res){
                    that.setData({
                      orderNum:res.data.ordernum,
                      storeNum: res.data.collnum
                    })
                  }
                })
              }
            })
          }
        })
      },
      fail: function () {

      }
    });
    if (app.globalData.userInfo){
      var that=this;
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo:true
      })
      wx.request({
        url: 'https://soya.91soya.com/api/ordernum/' + app.globalData.openId,
        method:"get",
        success:function(res){
          console.log(res)
          that.setData({
            orderNum:res.data.ordernum,
            storeNum: res.data.collnum
          })
        }
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        wx.request({
          url: 'https://soya.91soya.com/api/ordernum/' + app.globalData.openId,
          method: "get",
          success: function (res) {
            console.log(res)
            that.setData({
              orderNum: res.data.ordernum,
              storeNum: res.data.collnum
            })
          }
        })
      }
      
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          wx.request({
            url: 'https://soya.91soya.com/api/ordernum/' + app.globalData.openId,
            method: "get",
            success: function (res) {
              console.log(res)
              that.setData({
                orderNum: res.data.ordernum,
                storeNum: res.data.collnum
              })
            }
          })
        }
      })
      
    }
  },

  call: app.call

})